<!--
 * @Description: zzb
 * @Version: 0.0.1
 * @Autor: zzb
 * @Date: 2022-07-15 10:01:09
 * @LastEditors: zzb
 * @LastEditTime: 2022-12-20 11:45:24
-->

<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import { yearReadExport, yearRead } from "@/api/statistics/statistics";

import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import { mapState } from "vuex";
import resize from "../mixins/resize";

export default {
  name: "yearRead",
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "350px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    siteRow: {},
  },
  data() {
    return {
      chart: null,
      chartData: {},
    };
  },

  components: {},

  computed: {
    ...mapState({}),
  },

  mounted() {
    this.getList();
    console.log("siteRow", this.siteRow);
  },

  watch: {
    siteRow: {
      handler(val, oldVal) {
        console.log(
          "🚀 ~ file: yearRead.vue:64 ~ handler ~ val, oldVal",
          val,
          oldVal
        );
        this.getList();
      },
      deep: true,
      immediate: true,
    },
  },

  methods: {
    getList() {
      yearRead({ siteId: this.siteRow.siteId }).then((res) => {
        console.log("res: ", res);
        this.chartData = {
          month: [],
          num: [],
        };
        res.data.forEach((item) => {
          this.chartData.month.push(item.month.replace("年", "年\n"));
          this.chartData.num.push(item.num);
        });
        // this.chartData = {
        //   month: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
        //   num: [10, 11, 13, 11, 12, 12, 9],
        // };

        this.initChart();
      });
    },
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    },
    setOptions(obj) {
      this.chart.setOption({
        grid: {
          left: 30,
          right: 30,
          bottom: 20,
          top: 70,
          containLabel: true,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            crossStyle: {
              color: "#999",
            },
          },
        },
        toolbox: {
          show: true,
          feature: {
            magicType: { show: true, type: ["line", "bar"] },
            restore: { show: true },
            saveAsImage: { show: true },
          },
        },
        xAxis: {
          data: obj.month,
          type: "category",
          // axisTick: {
          //   show: false,
          // },
          boundaryGap: false, // 坐标轴两边留白策略，
          axisPointer: {
            type: "shadow",
          },
          // axisLine: {
          //   symbol: ["none", "arrow"],
          //   symbolOffset: 10,
          // },
        },
        yAxis: [
          {
            name: "阅读量(k)",
            // nameTextStyle: { align: "left" },
            // minInterval: 1,
            type: "value",
            // axisTick: {
            //   show: false,
            // },
            // axisLine: {
            //   symbol: ["none", "arrow"],
            //   symbolOffset: 10,
            // },
          },
        ],

        series: [
          {
            name: "阅读量",
            // smooth: true,
            type: "line",
            data: obj.num,
            markPoint: {
              data: [
                { type: "max", name: "Max" },
                { type: "min", name: "Min" },
              ],
            },
            //平均值
            // markLine: {
            //   data: [{ type: "average", name: "Avg" }],
            // },
            animationDuration: 2800,
          },
          // {
          //   name: "Highest",
          //   type: "line",
          //   data: [10, 11, 13, 11, 12, 12, 9],
          //   markPoint: {
          //     data: [
          //       { type: "max", name: "Max" },
          //       { type: "min", name: "Min" },
          //     ],
          //   },
          //   markLine: {
          //     data: [{ type: "average", name: "Avg" }],
          //   },
          // },
        ],
      });
    },
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
};
</script>

<style scoped lang='scss'>
</style>
